<%@page import="kr.cadis.clms.dao.LimitCashDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*, kr.cadis.clms.dao.*, kr.cadis.clms.util.Util" %>    
<%
	Calendar calendar = Calendar.getInstance();

	// 오늘이 이번 주의 몇번째 날인가? 요일은 숫자로 가져옴
	int intDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

	String dayOfWeek = Util.makeDayOfWeekToKorean(intDayOfWeek);
	
	LimitCashDao limitCashDao = new LimitCashDao();
	PersonDao personDao = new PersonDao();
	AbsenceDao absenceDao = new AbsenceDao();
	DailySpendDao dailySpendDao = new DailySpendDao();
	
	Date toDate = calendar.getTime();
	
	// 오늘은 스트링으로 만듬 ('yyyy-mm-dd')
	String strTheDate = Util.makeStringDate(calendar);
	
	int limitCash = limitCashDao.selectMoneyForTheDate(strTheDate);
	
	int countOfPerson = personDao.selectAllCount();
	
	///
	
	int absenceCount = 0;
	
	Calendar calendarMondayForThisWeek = Calendar.getInstance();

	// 이번 주의 월요일 찾기	
	calendarMondayForThisWeek.add(Calendar.DAY_OF_WEEK, 2 - intDayOfWeek);
	
	String strMondayOfThisWeek = calendarMondayForThisWeek.get(Calendar.YEAR) + "-" + (calendarMondayForThisWeek.get(Calendar.MONTH) + 1) + "-" + calendarMondayForThisWeek.get(Calendar.DATE);
	
	for(int i = 0; i < 5; i++) {
		String searchDate = calendarMondayForThisWeek.get(Calendar.YEAR) + "-" + (calendarMondayForThisWeek.get(Calendar.MONTH) + 1) + "-" + calendarMondayForThisWeek.get(Calendar.DATE);

		calendarMondayForThisWeek.add(Calendar.DAY_OF_WEEK, 1);
		
		absenceCount += absenceDao.selectAbsenceCountByDate(searchDate);
		
		//out.println(searchDate + " : " + absenceCount);
	}

	String strFridayOfThisWeek = calendarMondayForThisWeek.get(Calendar.YEAR) + "-" + (calendarMondayForThisWeek.get(Calendar.MONTH) + 1) + "-" + (calendarMondayForThisWeek.get(Calendar.DATE)-1);
	
	int totalPossibleMoney = limitCash * (countOfPerson * 5 - absenceCount); // 금주 사용 가능 총금액
	
	int totalSpendMoney = dailySpendDao.selectTotalSpendMoneyForThisWeek(strMondayOfThisWeek, strFridayOfThisWeek);
%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method = "post" action = "InsertDo.jsp">
<table border="1">
	<tr><th>일자</th><td><input name="spendDate" value="<%= strTheDate %>"></td></tr>
	<tr><th>요일</th><td><%= dayOfWeek %>요일</td></tr>
	<tr><th>금주 가용</th><td><%= String.format("%,d", totalPossibleMoney) %></td></tr>
	<tr><th>금주 사용</th><td><%= String.format("%,d", totalSpendMoney) %></td></tr>
	<tr><th>금주 잔액</th><td><%= String.format("%,d", totalPossibleMoney - totalSpendMoney) %></td></tr>
	<tr><th>오늘 사용</th><td><input name = "money" value = "24000"></td></tr>
</table>
<br />
<input type = "submit">
</form>
</body>
</html>